﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CRMMis.BLL;
using System.Data;
using Ext.Net;

public partial class CRM_Manager_ERP_CargoManage : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState_OrderSre = "CargoId DESC ";
            AddHandlersToControls();
            Stroebind();
        }
    }

    protected void AddHandlersToControls()
    {
        string Url = G_Url_allstring + "/CRM_Manager/ERP_CargoEdit.aspx";

        string tabid = base.G_FrameTabid + ",Store1";
        string handler = "popfortab(GridPanel1, '{0}', '{1}', '{2}', {3}, '{4}', '{5}', '{6}', {7});";
        extBtn_Add.Listeners.Click.Handler = string.Format(handler, Url, "ObjId", "Add", 0, "", tabid, "商品（详细）", 1);
        extBtn_Edit.Listeners.Click.Handler = string.Format(handler, Url, "ObjId", "CargoId", 0, "", tabid, "商品（详细）", 2);
        GridPanel1.Listeners.RowDblClick.Handler = string.Format(handler, Url, "ObjId", "CargoId", "rowIndex", "", tabid, "商品（详细）", 3);
    }

    #region 页面刷新和加载
    
    protected void OnData_Refresh(object sender, StoreRefreshDataEventArgs e)
    {
        int recordcount = CRMMis.BLL.ERP_Cargo.GetRecorderCount(GetQryString());
        BindGridPSData(e, ViewState_OrderSre, PagingToolBar1, Store1, recordcount);
    }


    public override DataTable GetPSData(int PageSize, int CurPage, string OrderStr)
    {
        try
        {
            ViewState["OrderStr"] = OrderStr;
            DataTable dt = CRMMis.BLL.ERP_Cargo.GetPageList(PageSize, CurPage, GetQryString(), OrderStr).Tables[0];
            return dt;
        }
        catch (Exception ex)
        {
            throw new Exception(ex.ToString());
        }
        finally
        {
            GC.Collect();
        }
       
    }

    private string GetQryString()
    {
        string result = " 1 = 1 and DelState<>1";
        if(!string.IsNullOrEmpty(exttxtf_CargoName.Text.Trim()))
            result += " and CargoName like '%"+ exttxtf_CargoName.Text.Trim() + "%' ";
        if (!string.IsNullOrEmpty(exttxtf_Spec.Text.Trim()))
            result += " and Spec like '%" + exttxtf_Spec.Text.Trim() + "%' ";
        if (!string.IsNullOrEmpty(exttxtf_Area.Text.Trim()))
            result += " and Area like '%" + exttxtf_Area.Text.Trim() + "%' ";
        if (!string.IsNullOrEmpty(exttxtf_ByName.Text.Trim()))
            result += " and ByName like '%" + exttxtf_ByName.Text.Trim() + "%' ";
        if (!string.IsNullOrEmpty(exttxtf_Version.Text.Trim()))
            result += " and Version like '%" + exttxtf_Version.Text.Trim() + "%' ";
        if (!string.IsNullOrEmpty(extcomb_Use.SelectedItem.Text.Trim()))
            result += " and UseId = '" + extcomb_Use.SelectedItem.Value.Trim() + "' ";
        if (!string.IsNullOrEmpty(exttxtf_CargoCode.Text.Trim()))
            result += " and CargoCode like '%" + exttxtf_CargoCode.Text.Trim() + "%' ";
        if (!string.IsNullOrEmpty(exttxtf_ProduceName.Text.Trim()))
            result += " and ProduceName like '%" + exttxtf_ProduceName.Text.Trim() + "%' ";
        return result;
    }

    protected void CargoDetail_Refresh(object sender, StoreRefreshDataEventArgs e)
    {
        if (SelectedCargoId.Value == null || string.IsNullOrEmpty(SelectedCargoId.Value.ToString()))
            return;
        DataTable dt = ERP_Cargo_Detail.GetList("CargoId="+ SelectedCargoId.Value.ToString() + "", "DetailId DESC").Tables[0];
        Store2.DataSource = dt;
        Store2.DataBind();
    }

    private void Stroebind()
    {
        DataTable dt = CRMMis.BLL.ERP_Use.GetList("","Use_ID ASC").Tables[0];
        Store3.DataSource = dt;
        Store3.DataBind();
        dt.Dispose();
    }
    #endregion

    #region 按钮事件
    protected void OnBtnSearchClick(object sender, DirectEventArgs e)
    {
        this.GridPanel1.Reload();
    }

    protected void extBtn_Refresh_Click(object sender, DirectEventArgs e)
    {
        this.GridPanel1.Reload();
    }

    protected void extBtn_Delete_Click(object sender, DirectEventArgs e)
    {
        int nCount = 0;
        RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
        nCount = sm.SelectedRows.Count;
        if (nCount == 0)
        {
            X.Msg.Alert("系统提示", "请先选择要删除的记录！").Show();
            return;
        }

        X.Msg.Confirm("系统提示", "是否删除选中行?", new MessageBoxButtonsConfig
        {
            Yes = new MessageBoxButtonConfig
            {
                Handler = "Ext.net.DirectMethods.DoDel();",
                Text = "是"
            },
            No = new MessageBoxButtonConfig
            {
                Text = "否"
            }
        }).Show();
    }

    [DirectMethod]
    public void DoDel()
    {
        RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
        try
        {

            var model = CRMMis.BLL.ERP_Cargo.GetModel(Convert.ToInt32(sm.SelectedRow));
            //判断是否有审核员角色
            if (!BS_LoginRoles.Contains("02")) //普通用户
            {
                if (model.State > 0)
                {
                    X.Msg.Alert("系统提示", "已提交的数据不能删除！").Show();
                    return;
                }
            }
            CRMMis.BLL.ERP_Cargo.DeletedCargo(sm.SelectedRow.RecordID, BS_LoginUserID);
            
        }
        catch(Exception ex)
        {
            X.Msg.Alert("系统提示", "删除记录失败！</br>" + ex.Message).Show();
            return;
        }
        finally
        {
            GC.Collect();
        }
        this.GridPanel1.Reload();
    }
    #endregion

}